package com.news.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.news.model.auto.News;
import com.news.model.auto.Role;
import com.news.service.IRoleService;
import com.news.service.impl.NewsServiceImpl;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 达瓦里氏
 * @since 2021-12-28
 */
@RestController
@RequestMapping("/news")
@CrossOrigin
public class NewsController {

    @Resource
    private NewsServiceImpl newsService;
    /*查询新闻*/
    @GetMapping("/getNews")
    public List<News> getNews(){
        return newsService.list();
    }

    /*查询国内咨询*/
    @GetMapping("/getInterNews")
    public List<News> getInterNews(){
        QueryWrapper<News> wrapper = new QueryWrapper<>();
        wrapper.eq("other",1);
        return newsService.list(wrapper);
    }
    /*查询国外咨询*/
    @GetMapping("/getExterNews")
    public List<News> getExterNews(){
        QueryWrapper<News> wrapper = new QueryWrapper<>();
        wrapper.eq("other",2);
        return newsService.list(wrapper);
    }

    /*通过id查询新闻*/
    @GetMapping("/getNewsById/{id}")
    public News getNewsById(@PathVariable("id") int id){
        return newsService.getById(id);
    }

    /*修改新闻*/
    @PostMapping("/updateNews")
    public void updateNews(@RequestBody News news){
        newsService.updateById(news);
    }

    /*添加新闻*/
    @PostMapping("/setNews")
    public void setNews(@RequestBody News news){
        newsService.save(news);
    }

    /*删除新闻*/
    @PostMapping("/deletedById/{id}")
    public boolean deleted(@PathVariable("id") int id){
        return newsService.removeById(id);
    }

    /*国内图片上传*/
    @PostMapping("/eluploadInter")
    public String elUploadInter(MultipartFile file){
        String fileName = file.getOriginalFilename();
        System.out.println("进来了");
        File dest = new File("D:\\project\\news\\vue-news\\static\\img\\inter\\" + fileName);
        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败！";
    }
    /*国际图片上传*/
    @PostMapping("/eluploadExter")
    public String elUploadExter(MultipartFile file){
        String fileName = file.getOriginalFilename();
        System.out.println("进来了");
        File dest = new File("D:\\project\\news\\vue-news\\static\\img\\exter\\" + fileName);
        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败！";
    }


}
